<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    //登录显示
    public function login()
    {
        return view('login');
    }

    //登录处理
    public function store(Request $request)
    {
        $data = $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:5'
        ]);
        //检查邮箱是否被
        if (!User::where('email', $request->email)->value('email_active')) {
            session()->flash('danger', '邮箱未验证，登录失败！');
            return redirect()->route('home');
        }
        if (\Auth::attempt($data)) {
            session()->flash('success', '登录成功！');
            return redirect()->route('home');
        }
        session()->flash('danger', '用户名或密码错误');
        return back();
    }

    //退出
    public function logout()
    {
        \Auth::logout();
        session()->flash('success', '退出成功！');
        return redirect()->route('login');
    }
}
